package com.servlet;

import com.entity.DinerGoods;
import com.entity.DinerOrder;
import com.entity.OrderSummary;
import com.entity.dto.ApiResult;
import com.entity.dto.LayuiPage;
import com.entity.dto.RuoyiResult;
import com.service.DinerOrderService;
import com.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping
public class DinerOrderServilet {

    @Autowired
    DinerOrderService dinerOrderService;


    /**
     * 展示订单信息
     */
    @RequestMapping(value = "getDinerOrderInfo")
    @ResponseBody
    @CrossOrigin
    public LayuiPage<DinerOrder> getDinerOrderInfo(HttpServletRequest request) {
        LayuiPage<DinerOrder> layuiPage = new LayuiPage<>();
        layuiPage.setMsg("OK");
        layuiPage.setData(dinerOrderService.getDinerOrderInfo(UserUtil.getUserId(request)));
        layuiPage.setType("Array");
        return layuiPage;
    }

    /**
     * 添加订单信息
     */
    @RequestMapping(value = "insertDinrORderInfo", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public ApiResult insertDinrORderInfo(@RequestBody DinerOrder dinerOrder, HttpServletRequest request) {
        ApiResult layuiPage = new ApiResult();
        String createBy = String.valueOf(UserUtil.getUserId(request));
        dinerOrder.setCreateBy(createBy);
        if (dinerOrderService.insertDinrORderInfo(dinerOrder) == 0) {

            layuiPage.setCode(300);
            layuiPage.setMsg("订单新增失败");

        } else {
            System.out.println(dinerOrder.getId());
            layuiPage.setData(dinerOrder.getId());
            layuiPage.setMsg("ok");
        }

        layuiPage.setType("Integer");
        return layuiPage;
    }

    /**
     * 修改订单状态，取消订单
     */
    @RequestMapping(value = "UpdateDinerrOrderState")
    @ResponseBody
    @CrossOrigin
    public LayuiPage<Integer> UpdateDinerrOrderState(DinerOrder dinerOrder) {
        LayuiPage<Integer> layuiPage = new LayuiPage<>();
        if (dinerOrderService.UpdateDinerrOrderState(dinerOrder) == 0) {
            layuiPage.setCode(202);
            layuiPage.setMsg("修改失败");
        } else {
            layuiPage.setMsg("ok");
        }
        layuiPage.setData(null);
        layuiPage.setType("Integer");
        return layuiPage;


    }
    @RequestMapping(value = "getDinerOrderThisInfo")
    @ResponseBody
    @CrossOrigin
    public LayuiPage<DinerOrder> getDinerOrderThisInfo(Integer orderId,HttpServletRequest request){
        LayuiPage<DinerOrder> layuiPage = new LayuiPage<>();
        layuiPage.setMsg("OK");
        layuiPage.setData(dinerOrderService.getDinerOrderThisInfo(UserUtil.getUserId(request),orderId));
        layuiPage.setType("Array");
        return layuiPage;
    }
    @RequestMapping(value = "getDinerOrderHistoryInfo")
    @ResponseBody
    @CrossOrigin
    public LayuiPage<DinerOrder> getDinerOrderHistoryInfo(Integer flag,HttpServletRequest request){

        LayuiPage<DinerOrder> layuiPage = new LayuiPage<>();
        layuiPage.setMsg("OK");
        layuiPage.setData(dinerOrderService.getDinerOrderHistoryInfo(flag, String.valueOf(UserUtil.getUserId(request))));
        layuiPage.setType("Array");
        return layuiPage;
    }

    @RequestMapping(value = "getDinerORderInfo")
    @ResponseBody
    @CrossOrigin
    public LayuiPage<DinerOrder> getDinerORderInfo(Integer orderId){
        LayuiPage<DinerOrder> layuiPage = new LayuiPage<>();
        layuiPage.setMsg("OK");
        layuiPage.setData(dinerOrderService.getDinerORderInfo(orderId));
        layuiPage.setType("Array");
        return layuiPage;
    }

    @RequestMapping(value = "getOrderSummary")
    @ResponseBody
    @CrossOrigin
    public RuoyiResult<OrderSummary> getOrderSummary(String goods_name, String create_time){
        RuoyiResult<OrderSummary> ruoyiResult = new RuoyiResult<>();
        ruoyiResult.setMsg("OK");
        List<OrderSummary> list =dinerOrderService.getOrderSummary(goods_name,create_time);
        ruoyiResult.setRows(list);
        ruoyiResult.setTotal(0);
        return ruoyiResult;
    }


}
